#include <sys/_stdint.h>

#include <cstdint>
#include <SI4735.h>


#define LSB 0
#define USB 1
#define BAND_SIZE 18

#define FM0 1
#define AM1 2
#define MW2 3
#define LW3 4
#define SW4 5
#define SW120 6
#define SW90 7
#define SW75 8
#define SW60 9
#define SW49 10
#define SW41 11
#define SW31 12
#define SW25 13
#define SW22 14
#define SW19 15
#define SW16 16
#define SW13 17
#define SW11 18
#define SSB18 19

typedef struct {
  char *freqName;
  uint16_t minimumFreq;
  uint16_t maximumFreq;
  uint16_t currentFreq;
  uint16_t currentStep;
  uint8_t usblsb;
} Band;


class Radio {
public:
  Radio();
  virtual ~Radio();
  void off();
  void on();
  void setFrequency(uint16_t val);
  uint16_t getFrequency();
  void autoFrequencyUp();
  void autoFrequencyDown();
  void frequencyUp();
  void frequencyDown();
  void status();
  void volumeUp();
  void volumeDown();
  uint16_t getVolume();
  void setVolume(uint16_t val);
  char *  change(uint8_t index=0);
  char *  change2(uint8_t index,void (*fn)());
  char * rds();
  bool isCurrentTuneFM();
  void bandWidth(int8_t bw);
  char * getBandWidth();
  void setSSB(uint8_t usblsb);
  uint8_t getSSB();
  void setAGC(uint8_t idx);
  void setAGC(uint8_t dis,uint8_t idx);
  void setAvcAmMaxGain(uint8_t gain=90);
  uint8_t getAvcAmMaxGain();
  uint8_t getAGC();
  void setSSBBfo(int bfo);
  int getSSBBfo();
  bool isSSB();
  bool isFM();
  bool isAM();
  char * getFreqName();
  uint8_t getSnr();
  uint8_t getRssi();
  //void static showFrequency(SI4735 *rx,uint16_t freq);
protected:
  SI4735 *_rx;
  uint16_t _fromFreq;
  uint16_t _toFreq;
  uint16_t _initialFreq;
  uint16_t _step;
  uint16_t volume = 55;
  uint16_t _currentFreq = _initialFreq;
  bool _isSSB = false;
  bool _isFM = true;
  bool _isAM = false;
  uint8_t _usbLsb = LSB;
  uint8_t _agc = 0;
  long pulling_rds = millis();
private:
   char* freqName;
  int _bfo = 0;
  int8_t ssb_bandwidthIdx = 0;
  int8_t bandwidthIdx = 0;
  char * currentBandWidth = "";
  uint8_t bandIndex = 0;
  char *ssb_bandwidth[6] = {"1.2", "2.2", "3.0", "4.0", "0.5", "1.0"};
  char *bandwidth[7] = {"6", "4", "3", "2", "1", "1.8", "2.5"};
  Band _band[BAND_SIZE] = {
    { "FM", 6400, 10800, 9750, 10, 0 },
    { "AM", 520, 1750, 520, 10, 0 },
    { "LW", 100, 279, 153, 5, 0 },
    { "SW", 2300, 30000, 2300, 5, 0 },
    { "120m", 2300, 2490, 2300, 5, 0 },
    { "90m", 3200, 3400, 3200, 5, 0 },
    { "75m", 3900, 4000, 3900, 5, 0 },
    { "60m", 4700, 5200, 4700, 5, 0 },
    { "49m", 5700, 6200, 5700, 5, 0 },
    { "41m", 7100, 7300, 7100, 5, 0 },
    { "31m", 9500, 9775, 9500, 5, 0 },
    { "25m", 11700, 11975, 11750, 5, 0 },
    { "22m", 13500, 13900, 13600, 5, 0 },
    { "19m", 15100, 15450, 15100, 5, 0 },
    { "16m", 17700, 17900, 17700, 5, 0 },
    { "13m", 21450, 21750, 21500, 5, 0 },
    { "11m", 25600, 25600, 26100, 5, 0 },
    { "SSB", 520, 28500, 7050, 1, LSB }  
  };
};
